﻿"use strict";
Math.clamp = function (v, min, max) {
    const mib = (typeof (min) == "number") ? [min] : min;
    const mxb = (typeof (max) == "number") ? [max] : max;
    return Math.min(Math.max(v, ...mib), ...mxb);
};
Math.dist2d = function (x1, x2, y1, y2) {
    return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
};
Math.dist = function (x1, x2) {
    return Math.abs(x2 - x1);
};
Math.signed = function (x) {
    return x < 0;
};
Math.limitRandom = function (min, max = 65536) {
    return Math.random() * (max - min) + min;
};
Math.intRandom = function (min = -32768, max = 32767) {
    return Math.round(Math.limitRandom(min, max));
};
Math.rads = function (deg) {
    return deg ? (Math.PI / 180) * deg : 0;
};
function setNumberBit(v, order, bit) {
    if (!bit) {
        bit = false;
    }
    return v | (+bit << order);
}
function getNumberBit(v, order) {
    return !!(v | (1 << order));
}
function setBigintBit(v, order, bit) {
    if (!bit) {
        bit = false;
    }
    return v | (BigInt(bit) << order);
}
function getBigintBit(v, order) {
    return !!(v | (1n << order));
}
function safeSet(src, dest, prop) {
    if (dest[prop] !== undefined) {
        src[prop] = dest[prop];
    }
}
function safeCopyExtend(origin, child) {
    let result = {};
    for (const key in origin) {
        if (Object.prototype.hasOwnProperty.call(origin, key)) {
            if (child[key] !== undefined) {
                result[key] = child[key];
            }
            else if (origin[key] !== undefined) {
                result[key] = origin[key];
            }
        }
    }
    return result;
}
function safeExtend(origin, child) {
    for (const key in origin) {
        if (Object.prototype.hasOwnProperty.call(origin, key)) {
            if (child[key] !== undefined) {
                origin[key] = child[key];
            }
        }
    }
    return origin;
}
function gNullIE(src, key) {
    if (!src) {
        return null;
    }
    if (!src[key]) {
        return null;
    }
    return src[key];
}
function gNullIEForNumber(src, key) {
    if (!src) {
        return 0;
    }
    if (!src[key]) {
        return 0;
    }
    return src[key];
}
function gNullIEForString(src, key) {
    if (!src) {
        return "";
    }
    if (!src[key]) {
        return "";
    }
    return src[key];
}
function setToReadonly(o, p, v) {
    o[p] = v;
}
function charIs(ch, ...chl) {
    if (ch === undefined) {
        return false;
    }
    for (let i = 0; i < chl.length; i++) {
        if (chl[i] == ch) {
            return true;
        }
    }
    return false;
}
function numberToUInt(v) {
    return Math.max(~~v, 0);
}
function numberToInt(v) {
    return ~~v;
}
var SDT;
(function (SDT) {
    function Color(r, g, b, a = 255) {
        let c = new BitColor();
        c.set(r, g, b, a);
        return c;
    }
    SDT.Color = Color;
    class BitColor {
        _Data;
        constructor(v) {
            Object.setPrototypeOf(Object.getPrototypeOf(this) ?? this, null);
            if (v) {
                this.setBinary(v);
            }
        }
        getBinary() {
            return this._Data;
        }
        setBinary(v) {
            this._Data = v;
        }
        copy() {
            let r = new BitColor();
            r.setBinary(this._Data);
            return r;
        }
        set(red, green, blue, alpha = 0xFF) {
            this._Data = 0;
            this._Data |= red & 0x000000FF;
            this._Data |= (green << 8) & 0x0000FF00;
            this._Data |= (blue << 16) & 0x00FF0000;
            this._Data |= (alpha << 24) & 0xFF000000;
        }
        has() {
            return true;
        }
        remove() {
            this._Data = 0;
        }
        negative() {
            let r = new BitColor(~this._Data);
            r.alpha = this.alpha;
            return r;
        }
        isEqual(v) {
            return this.alpha === v.alpha &&
                this.red === v.red &&
                this.green === v.green &&
                this.blue === v.blue;
        }
        get red() {
            return this._Data & 0x000000FF;
        }
        set red(v) {
            this._Data = (this._Data & 0xFFFFFF00) | (v & 0xFF);
        }
        get green() {
            return (this._Data & 0x0000FF00) >> 8;
        }
        set green(v) {
            this._Data = (this._Data & 0xFFFF00FF) | ((v & 0xFF) << 8);
        }
        get blue() {
            return (this._Data & 0x00FF0000) >> 16;
        }
        set blue(v) {
            this._Data = (this._Data & 0xFF00FFFF) | ((v & 0xFF) << 16);
        }
        get alpha() {
            return (this._Data & 0xFF000000) >>> 24;
        }
        set alpha(v) {
            this._Data = (this._Data & 0x00FFFFFF) | ((v & 0xFF) << 24);
        }
        static BIT_SIZEOF = 32;
        toString() {
            let r = this.red.toString(16);
            if (r.length == 1) {
                r = '0' + r;
            }
            let g = this.green.toString(16);
            if (g.length == 1) {
                g = '0' + g;
            }
            let b = this.blue.toString(16);
            if (b.length == 1) {
                b = '0' + b;
            }
            let a = this.alpha.toString(16);
            if (a.length == 1) {
                a = '0' + a;
            }
            if (a.toLowerCase() === "FF") {
                return `#${r}${g}${b}`;
            }
            return `#${r}${g}${b}${a}`;
        }
    }
    SDT.BitColor = BitColor;
    let Symbols;
    (function (Symbols) {
        Symbols.REG_ITEM_LIST = Symbol("SDT.Registry@itemList");
        Symbols.REG_ITEM_COUNT = Symbol("SDT.Registry@itemCount");
        Symbols.REG_DATA_FILTER = Symbol("SDT.Registry@filter");
        Symbols.REG_CACHE_KEYS = Symbol("SDT.Registry@cache.KEYS");
        Symbols.REG_CACHE_ITEMS = Symbol("SDT.Registry@cache.ITEMS");
    })(Symbols || (Symbols = {}));
    class Registry {
        [Symbols.REG_ITEM_LIST];
        [Symbols.REG_ITEM_COUNT];
        [Symbols.REG_CACHE_KEYS];
        [Symbols.REG_CACHE_ITEMS];
        [Symbols.REG_DATA_FILTER];
        isEqual;
        constructor(filter, isEqual) {
            Object.setPrototypeOf(Object.getPrototypeOf(this) ?? this, null);
            this[Symbols.REG_ITEM_LIST] = {};
            this[Symbols.REG_ITEM_COUNT] = 0;
            if (!filter) {
                filter = () => {
                    return true;
                };
            }
            this[Symbols.REG_DATA_FILTER] = filter;
            if (!isEqual) {
                isEqual = (v, v2) => {
                    return v === v2;
                };
            }
            this.isEqual = isEqual;
            this[Symbols.REG_CACHE_KEYS] = null;
            this[Symbols.REG_CACHE_ITEMS] = null;
        }
        _Create_Note(base) {
            let r = {
                value: base.value,
                ...this._Create_Access(base)
            };
            Object.setPrototypeOf(r, null);
            return r;
        }
        _Create_Access(base) {
            let r = {
                readonly: base.readonly ?? false,
                configurable: base.configurable ?? true,
                deletable: base.deletable ?? true,
                enumerable: base.enumerable ?? true
            };
            Object.setPrototypeOf(r, null);
            return r;
        }
        write(key, value, acces = {}) {
            if (!this.registr(key, value, acces)) {
                let r = this.writeValue(key, value);
                if (r) {
                    return this.modify(key, acces);
                }
            }
            return false;
        }
        writeValue(key, value) {
            if (value !== undefined) {
                if (!this[Symbols.REG_DATA_FILTER](value)) {
                    return false;
                }
                if (!this.registrValue(key, value)) {
                    return this.replace(key, value);
                }
            }
            return false;
        }
        replace(key, value) {
            if (value !== undefined) {
                if (!this[Symbols.REG_DATA_FILTER](value)) {
                    return false;
                }
                if (this.has(key)) {
                    if (!this[Symbols.REG_ITEM_LIST][key]?.readonly) {
                        this[Symbols.REG_ITEM_LIST][key].value = value;
                        return true;
                    }
                }
            }
            return false;
        }
        modify(key, config) {
            let c = this.getConfig(key);
            if (c) {
                if (c.configurable) {
                    Object.setPrototypeOf(config, null);
                    safeExtend(c, config);
                    return true;
                }
            }
            return false;
        }
        static generateKey() {
            return crypto.randomUUID();
        }
        registr(key, value, access = {}) {
            if (this.has(key)) {
                return false;
            }
            this[Symbols.REG_ITEM_COUNT]++;
            this[Symbols.REG_ITEM_LIST][key] = this._Create_Note({
                value: value,
                ...access
            });
            this[Symbols.REG_CACHE_KEYS] = null;
            this[Symbols.REG_CACHE_ITEMS] = null;
            return true;
        }
        registrValue(key, value) {
            return this.registr(key, value);
        }
        has(key) {
            return Object.hasOwn(this[Symbols.REG_ITEM_LIST], key);
        }
        read(key) {
            if (this.has(key)) {
                return this[Symbols.REG_ITEM_LIST][key]?.value;
            }
            return undefined;
        }
        getConfig(key) {
            if (this.has(key)) {
                return this._Create_Access(this[Symbols.REG_ITEM_LIST][key]);
            }
            return undefined;
        }
        remove(key) {
            if (this.has(key)) {
                if (!this.getConfig(key)?.deletable) {
                    return false;
                }
                this[Symbols.REG_ITEM_COUNT]--;
                delete this[Symbols.REG_ITEM_LIST][key];
                return true;
            }
            this[Symbols.REG_CACHE_KEYS] = null;
            this[Symbols.REG_CACHE_ITEMS] = null;
            return false;
        }
        append(access, ...value) {
            let ra = [];
            value.forEach(v => {
                const r = Registry.generateKey();
                if (!this.registr(r, v, access)) {
                    ra.push(undefined);
                }
                ra.push(r);
            });
            return ra;
        }
        appendValue(...value) {
            return this.append({}, ...value);
        }
        search(v) {
            let r = [];
            const arr = this.itemKeys();
            arr.forEach(e => {
                const val = this.read(e);
                if (this.isEqual(v, val)) {
                    r.push(e);
                }
            });
            return r;
        }
        copyReference(key, source, access = {}) {
            if (this.has(key)) {
                return false;
            }
            let own = this[Symbols.REG_ITEM_LIST][source];
            if (!own) {
                return false;
            }
            this.registr(key, own.value, {
                configurable: own.configurable && access.configurable,
                deletable: own.deletable && access.deletable,
                enumerable: own.enumerable && access.enumerable,
                readonly: own.readonly || access.readonly
            });
            return true;
        }
        itemKeys() {
            let r = [];
            if (this[Symbols.REG_CACHE_KEYS]) {
                return this[Symbols.REG_CACHE_KEYS];
            }
            for (const key in this[Symbols.REG_ITEM_LIST]) {
                if (Object.prototype.hasOwnProperty.call(this[Symbols.REG_ITEM_LIST], key)) {
                    const element = this[Symbols.REG_ITEM_LIST][key];
                    if (element?.enumerable) {
                        r.push(key);
                    }
                }
            }
            this[Symbols.REG_CACHE_KEYS] = r;
            return r;
        }
        items() {
            let r = [];
            if (this[Symbols.REG_CACHE_ITEMS]) {
                return this[Symbols.REG_CACHE_ITEMS];
            }
            for (const key in this[Symbols.REG_ITEM_LIST]) {
                if (Object.prototype.hasOwnProperty.call(this[Symbols.REG_ITEM_LIST], key)) {
                    const element = this[Symbols.REG_ITEM_LIST][key];
                    if (element?.enumerable) {
                        r.push(element.value);
                    }
                }
            }
            this[Symbols.REG_CACHE_ITEMS] = r;
            return r;
        }
        [Symbol.iterator]() {
            const owner = this;
            return {
                index: 0,
                keys: this.itemKeys(),
                next() {
                    if (this.index == this.keys.length) {
                        return {
                            done: true,
                            value: undefined
                        };
                    }
                    const key = this.keys[this.index];
                    let r = owner.read(key);
                    this.index++;
                    return {
                        value: {
                            index: this.index,
                            key: key,
                            value: r
                        }
                    };
                }
            };
        }
        get length() {
            return this[Symbols.REG_ITEM_COUNT];
        }
        clear() {
            this[Symbols.REG_CACHE_ITEMS] = [];
            this[Symbols.REG_CACHE_KEYS] = [];
            this[Symbols.REG_ITEM_COUNT] = 0;
            this[Symbols.REG_ITEM_LIST] = Object.create(null);
        }
    }
    SDT.Registry = Registry;
    SDT.SpaceCharsRegistry = new Registry((v) => {
        return v.length == 1;
    });
    SDT.SpaceCharsRegistry.append({
        deletable: false,
        readonly: true,
        configurable: false,
        enumerable: true
    }, ' ', '\t', '\n', '\u200B', '\r', ' ', ' ', ' ');
})(SDT || (SDT = {}));
function pickWord(str, index, options = {}) {
    let iarr = SDT.SpaceCharsRegistry.items();
    let defstr = "";
    iarr.forEach(e => {
        defstr += e;
    });
    const opts = {
        blackList: options.blackList ?? defstr,
        whiteList: options.whiteList ?? "",
        nullableReset: options.nullableReset ?? true
    };
    function checkChar(ch) {
        if (!ch) {
            return false;
        }
        if (charIs(ch, ...opts.blackList)) {
            if (!charIs(ch, ...opts.whiteList)) {
                return false;
            }
        }
        return true;
    }
    if (!checkChar(str[index])) {
        return opts.nullableReset
            ? ""
            : str[index] !== undefined
                ? str[index]
                : "";
    }
    let wbegin = index;
    for (let it = index - 1; it >= 0; it--) {
        if (!checkChar(str[it])) {
            break;
        }
        wbegin = it;
    }
    let wend = index;
    for (let it = index + 1; it < str.length; it++) {
        if (!checkChar(str[it])) {
            break;
        }
        wend = it;
    }
    return str.substring(wbegin, wend + 1);
}
function wait(ms) {
    return new Promise((returnFunction) => {
        setTimeout(() => {
            returnFunction();
        }, ms);
    });
}
//# sourceMappingURL=Client.Data.js.map